import time
import traceback

from applications.configs.func import chinese_to_num


def check_two_url(url1, url2):
    url1 = str(url1).strip("#a").strip("_1/").replace("https://sk.3qdu.com/", "https://sk.3qxsw.org/")
    url2 = str(url2).strip("#a").strip("_1/").replace("https://sk.3qdu.com/", "https://sk.3qxsw.org/")
    print("url1", url1, "url2", url2)
    if url2 != url1:
        return False
    return True


# 3q读书网
class ThreeQRead:
    # 查询小说网址
    search_books_url = "https://sk.3qdu.com/"

    # 基础网站
    base_url = "https://sk.3qdu.com"

    # 小说基础章节
    charter_base_url = "https://sk.3qxsw.org"

    # 已抓取的数据
    used_chapters = []

    #
    pre_chapter = 0
    next_chapter = 0
    url_end_order_last = 0

    # 搜索书籍
    def search_books(self, base_object, novel_name):
        """
            搜索书籍
        :param base_object:   父类对象
        :param novel_name:    小说名字
        :return:
        """
        # 打开网页, 并搜索
        base_object.send_message_chick(self.search_books_url, "CLASS_NAME", "input1", novel_name)
        contents = base_object.parse_select(".search_list > a")
        result_contents = []
        # 限制获取5个
        number = 0
        for a in contents:
            if number >= 5:
                break
            address = self.base_url + a.attrs["href"]
            # # 获取详情
            novel = base_object.wait_css_show(address, "CSS_SELECTOR", ".tab")[0]
            p1 = novel.select(".p1")
            name = p1[0].text
            author = str(p1[1].text).strip("作者：")
            result_contents.append({"name": name, "address": address, "author": author, "chapter": 1})

            number += 1

        return result_contents

    # 获取章节信息
    def get_characters(self, base_object, novel_url, start_chapter, end_chapter):
        """
            获取章节信息
        :param base_object:     父类对象
        :param novel_url:       小说地址
        :param start_chapter:   开始章节
        :param end_chapter:     结束章节
        :return:
        """
        chapters_obj = base_object.wait_css_show(novel_url, "CLASS_NAME", "info_chapters", "-1", True)
        chapter_list = []
        if not chapters_obj:
            return chapter_list

        # 解析章节地址
        info_chapters = chapters_obj.select(".info_chapters>.p2")[-1]
        now_last_chapter = 0  # 当前章节
        for a in info_chapters.select("ul>li>a"):
            try:
                chapter = int(chinese_to_num(str(a.text).split(" ")[0].strip("第").strip("章")))
                now_last_chapter = chapter  # 当前章节
                if (start_chapter <= chapter <= end_chapter) and chapter not in self.used_chapters:
                    self.used_chapters.append(chapter)
                    chapter_list.append({"name": a.text, "address": self.base_url + a.attrs["href"], "chapter": chapter})
                else:
                    if chapter > end_chapter:
                        break
            except Exception:
                print(traceback.format_exc())
                continue

        # print("抓取章节数据", novel_url, now_last_chapter)
        # 最后章节未结束则继续
        now_last_chapter = now_last_chapter + 1 if now_last_chapter >= start_chapter else start_chapter

        # # 上一页页码不符合则跳过
        # if self.pre_chapter <= now_last_chapter <= self.next_chapter:
        #     now_last_chapter = self.next_chapter + 1
        if self.url_end_order_last <= now_last_chapter and self.url_end_order_last != 0:
            now_last_chapter = self.url_end_order_last + 1

        if now_last_chapter < self.url_end_order_last:
            # 查找需要的章节地址
            option_list = chapters_obj.select(".info_chapters>.listpage>.middle>select>option")
            if not option_list or len(option_list) <= 0:
                option_list = chapters_obj.select("#indexselect>option")    # 新页面数据不一样
            # print("抓取章节数据-----111111111", option_list)
            href = None
            for option in option_list:
                chapter_str = str(option.text).split("-")
                url_start_order = int(chapter_str[0].strip())
                url_end_order = int(chapter_str[1].strip().strip("章"))
                # print("抓取章节数据-----下一章地址", url_end_order, now_last_chapter, url_start_order)
                if url_end_order >= now_last_chapter >= url_start_order:
                    self.pre_chapter = url_start_order
                    self.next_chapter = url_end_order
                    href = option.attrs["value"] if 'http' in option.attrs["value"] else "https://sk.3qxsw.org" + option.attrs["value"]
                    break
            
            # 选择最后一张的最后一节
            option_list_last = option_list[-1]
            chapter_str_last = str(option_list_last.text).split("-")
            self.url_end_order_last = int(chapter_str_last[1].strip().strip("章")) - 1

            # 跳转下一页抓取数据
            if href:
                chapter_list = chapter_list + self.get_characters(base_object, href, now_last_chapter, end_chapter)

        return chapter_list

    # 获取小说内容
    def get_content(self, base_object, novel_url):
        """
            获取小说内容
        :param base_object:     父类对象
        :param novel_url:       小说地址
        :return:
        """
        # 获取小说内容
        soup = base_object.wait_css_show(novel_url, "CSS_SELECTOR", "#novelcontent>p", "-1")
        contents = soup.select("#novelcontent>p")
        for i in range(10):
            a = soup.select(".p4")[0]
            if a.text == '下一页':
                soup = base_object.wait_css_show(self.charter_base_url + a.attrs["href"], "CSS_SELECTOR", "#novelcontent>p", "-1")
                contents = contents + soup.select("#novelcontent>p")
            else:
                break
        return "\n".join([c.text for c in contents])

    def get_last_new_chapter(self, base_object, novel_url):
        """
        获取最新章节
        :param base_object:
        :param novel_url:
        :return:
        """
        # 获取章节信息
        new_chapters = base_object.wait_css_show(novel_url, "CLASS_NAME", "info_chapters", "-1")
        a_list = new_chapters.select(".info_chapters>.p2>li>a")
        chapter = 0
        for a in a_list:
            try:
                chapter = int(str(a.text).split(" ")[0].strip("第").strip("章"))
                if chapter:
                    break
            except:
                continue
        return chapter